
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :Sindy
	CreateTime:2008-02-13 12:20:35
*/
public class ProductCategorySrv extends IHandler
{

	public ProductCategorySrv() 
	{
		super();
	}
	private void deleteProductCategory(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		ProductCategory obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = ProductCategory.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveProductCategory(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		ProductCategory obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = ProductCategory.fetch();
    	}
    	else                     //update
    	{
    		obj = ProductCategory.fetch(con,id);
    	}
		ProductCategoryPageData data = (ProductCategoryPageData)PageData.fromForm(context,ProductCategoryPageData.class);
		context.setValue("ProductCategoryPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listProductCategory(DBConnection con,SrvContext context,String path) throws Exception
	{
		ProductCategorySearchEvent event = (ProductCategorySearchEvent)SearchEvent.fromForm(context,ProductCategorySearchEvent.class);
		context.setSessionValue("ProductCategorySearchEvent",event);
		context.setNextUrl(path+"ProductCategoryList.jsp");
	}
	private void backProductCategory(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"ProductCategoryList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listProductCategory"))
		{
			listProductCategory(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewProductCategory"))
		{
			String id = context.getString("Id",null);
			ProductCategory obj = ProductCategory.fetch(con,id);
			context.setValue("ProductCategory",obj);
			context.setNextUrl(path+"ProductCategoryView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteProductCategory"))
		{
			context.setErrorUrl(path+"ProductCategoryList.jsp");
			deleteProductCategory(con,context);
			backProductCategory(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddProductCategory"))
		{
			ProductCategory obj = ProductCategory.fetch();
			ProductCategoryPageData data = (ProductCategoryPageData)PageData.fromTable(obj,ProductCategoryPageData.class);
			context.setValue("ProductCategoryPageData",data);
			context.setNextUrl(path+"ProductCategoryEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateProductCategory"))
		{
			String id = context.getString("Id",null);
			ProductCategory obj = ProductCategory.fetch(con,id);
			ProductCategoryPageData data = (ProductCategoryPageData)PageData.fromTable(obj,ProductCategoryPageData.class);
			context.setValue("ProductCategoryPageData",data);
			context.setNextUrl(path+"ProductCategoryEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyProductCategory"))
		{
			String id = context.getString("Id",null);
			ProductCategory obj = ProductCategory.copy(ProductCategory.fetch(con,id));
			ProductCategoryPageData data = (ProductCategoryPageData)PageData.fromTable(obj,ProductCategoryPageData.class);
			context.setValue("ProductCategoryPageData",data);
			context.setNextUrl(path+"ProductCategoryEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveProductCategory"))
		{
			context.setErrorUrl(path+"ProductCategoryEdit.jsp");
			saveProductCategory(con,context,loginContext);
			backProductCategory(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewProductCategory"))
		{
			context.setErrorUrl(path+"ProductCategoryEdit.jsp");
			saveProductCategory(con,context,loginContext);
			ProductCategory obj = ProductCategory.fetch();
			ProductCategoryPageData data = (ProductCategoryPageData)PageData.fromTable(obj,ProductCategoryPageData.class);
			context.setValue("ProductCategoryPageData",data);
			context.setNextUrl(path+"ProductCategoryEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListProductCategory"))
		{
			backProductCategory(con,context,path);
			return;
		}
		
		//add by Sindy start

		//add by Sindy end

		Logger.throwException("ProductCategorySrv","unknown action_code="+action_code);
	}
}
